package com.common.web.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.common.web.service.JwtTokenUtil;

/**
 * @author liuchao
 * 控制器寄出来
 */
public class BaseController {

	@Autowired
	private JwtTokenUtil jwtTokenUtil;
	
	@Autowired
	private HttpServletRequest request;
	
	protected final HttpServletRequest getRequest() {
		 ServletRequestAttributes attributes= (ServletRequestAttributes)
				 RequestContextHolder.currentRequestAttributes();
		 return attributes.getRequest();
	}

	protected final HttpServletResponse getResponse() {
		ServletRequestAttributes attributes= (ServletRequestAttributes)
				RequestContextHolder.currentRequestAttributes();
		return attributes.getResponse();
	}
	
	protected final HttpSession getSession() {
		 ServletRequestAttributes attributes= (ServletRequestAttributes)
				 RequestContextHolder.currentRequestAttributes();
		 return attributes.getRequest().getSession();
	}

	/**
	 * 从token中获取登录用户编码
	 */
	protected String getUserCode() {
		return jwtTokenUtil.getUserCode(request);
	}

	/**
	 * 从token中获取登录用户名称
	 */
	protected String getUserName() {
		return jwtTokenUtil.getUserName(request);
	}
	
	/**
	 * 从token中获取登录用户编码和名称
	 */
	protected String[] getUserCodeAndName() {
		return jwtTokenUtil.getUserCodeAndName(request);
	}
	
}
